﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;

namespace DataAccess
{
    public class PersonalVehicleDataAccess
    {
        public bool InsertVehicle(PersonalVehicleInfo p)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var per = db.PersonalVehicles.Where(p1 => p1.ControlPlate.Equals(p.ControlPlate)).SingleOrDefault();
            if (per == null)
            {
                PersonalVehicle pv = new PersonalVehicle();
                pv.PerVehicleID = p.PerVehicleID;
                pv.CustomerID = p.CustomerID;
                pv.VehicleID = p.VehicleID;
                pv.ControlPlate = p.ControlPlate;
                pv.UsedYear = p.UsedYear;
                pv.Price = p.Price;

                db.PersonalVehicles.InsertOnSubmit(pv);
                db.SubmitChanges();
                return true;
            }
            else
            {
                return false;
            }
        }

        public PersonalVehicleInfo getPerVehicleByControlPlate(string controlPlate)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var vehicle = db.PersonalVehicles.Where(v => v.ControlPlate.Equals(controlPlate)).SingleOrDefault();
            if (vehicle != null) //found
            {
                PersonalVehicleInfo pvi = new PersonalVehicleInfo();
                pvi.PerVehicleID = vehicle.PerVehicleID;
                pvi.CustomerID = Convert.ToInt32(vehicle.CustomerID);
                pvi.VehicleID = Convert.ToInt32(vehicle.VehicleID);
                pvi.ControlPlate = vehicle.ControlPlate;
                pvi.UsedYear = Convert.ToInt32(vehicle.UsedYear);
                pvi.Price = Convert.ToSingle(vehicle.Price);
                return pvi;
            }
            else
            {
                return null;
            }
        }
    }
}
